package servlet;


import entity.Animal;
import service.AnimalService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/animal")
public class AnimalServlet extends HttpServlet {
    private AnimalService animalService;

    @Override
    public void init() throws ServletException {
        animalService = new AnimalService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idParam = request.getParameter("id");
        if (idParam == null) {
            List<Animal> animals = animalService.getAllAnimals();
            request.setAttribute("animals", animals);
            request.getRequestDispatcher("/adminIndex.jsp").forward(request, response);
        } else {
            int animalId = Integer.parseInt(idParam);
            Animal animal = animalService.getAnimalById(animalId);
            request.setAttribute("animal", animal);
            request.getRequestDispatcher("/animalDetail.jsp").forward(request, response);
        }
    }
}
